home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6482 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: ix.netcom.com!netnews
  2. From: scutler@ix.netcom.com (Scott Cutler)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sqrt() cheesy question
  5. Date: Sun, 25 Feb 1996 01:30:04 GMT
  6. Organization: Netcom
  7. Message-ID: <312fbb8c.7016394@nntp.ix.netcom.com>
  8. References: <4gjmmt$lt7@hopi.dtcc.edu>
  9. NNTP-Posting-Host: sac-ca9-22.ix.netcom.com
  10. X-NETCOM-Date: Sat Feb 24  5:31:09 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. On 23 Feb 1996 01:26:37 -0500, w4582@hopi.dtcc.edu (Rob Wiltbank)
  14. wrote:
  15.  
  16. >Howdy all.  I'm in a bind.
  17. >
  18. >I'm in a beggining C course at school, and I'm about 2 weeks ahead of the 
  19. >class so I'm doing projects that the class hasn't even considered going 
  20. >over yet.  I'm still on the very newbie side of it all.
  21. >
  22. >Could someone post the function definition of the sqrt() function found 
  23. >in math.h?
  24. >
  25. >Thanks.
  26. >
  27. >Rob
  28.  
  29. I believe that most of the functions in math.h are written in assembly
  30. language, so you probably wouldn't understand them.  However, if
  31. you're looking for a function that takes the square root, try this:
  32.  
  33. double sqrt(double z)
  34. {
  35. int
  36.     count;
  37. double
  38.     y=0,
  39.     x=1,
  40.     precision=.000000000001;
  41. while(1)
  42.     {
  43.     x=x-(x*x-z)/(2*x)
  44.     if(abs(z-x*x)<precision) break;
  45.     }
  46.  
  47. return x;
  48. }
  49. It's not super-efficient, but it works.
  50.  
  51. Scott Cutler SCutler@ix.netcom.com
  52.